using UnityEngine;
using QFramework;

namespace ProjectSurviver
{
	public partial class SimpleAxe : ViewController
	{
		private float mCurrentScends = 0;

		void Start()
		{
			// Code Here
		}

        void Update()
        {
            mCurrentScends += Time.deltaTime;

			if(mCurrentScends >= 1.0f)
			{
				Axe.Instantiate()
				.Show()
				.Position(this.Position())
				.Self(self =>
				{
					var rigibody2D = self.GetComponent<Rigidbody2D>();
					var randomX = RandomUtility.Choose(-8, -5, -3, 3, 5, 8);
					var randomY = RandomUtility.Choose(3, 5, 8);
					rigibody2D.velocity = new Vector2(randomX, randomY);

					self.OnTriggerEnter2DEvent(collider =>
					{
						var hurtBox = collider.GetComponent<HurtBox>();
						if(hurtBox)
						{
							if(hurtBox.Owner.CompareTag("Enemy"))
							{
								hurtBox.Owner.GetComponent<Enemy>().Hurt(2);
							}
						}
					}).UnRegisterWhenGameObjectDestroyed(self);

					ActionKit.OnUpdate.Register(() =>
					{
						if(Player.Default)
						{
							if(Player.Default.Position().y - self.Position().y > 15)
							{
								self.DestroyGameObjGracefully();
							}
						}
					}).UnRegisterWhenGameObjectDestroyed(self);
				});

				mCurrentScends = 0;
			}
        }
    }
}
